<template>
    <div class="container">

        <!-- 基本搜索+功能按钮 -->
        <el-form ref="searchForm1" :inline="true" :model="searchForm" size="medium" label-width="70px">
            <div class="table_tool_bar_wrap">
                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="订单状态" prop="status">
                            <el-radio-group v-model="searchForm.status" @change="handleChangeStatus" size="small">
                                <el-radio-button label="">全部</el-radio-button>
                                <el-radio-button :label="0">申请退款</el-radio-button>
                                <el-radio-button :label="1">商家拒绝</el-radio-button>
                                <el-radio-button :label="2">商品待退货</el-radio-button>
                                <el-radio-button :label="3">商家待收货</el-radio-button>
                                <el-radio-button :label="4">商家拒收货</el-radio-button>
                                <el-radio-button :label="5">等待退款</el-radio-button>
                                <el-radio-button :label="6">退款成功</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </div>
                </div>

                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="时间选择" prop="dateAttr">
                            <el-radio-group v-model="searchForm.dateAttr" @change="handleChangeDateAttr" size="small">
                                <el-radio-button label="">全部</el-radio-button>
                                <el-radio-button :label="0">今天</el-radio-button>
                                <el-radio-button :label="1">昨天</el-radio-button>
                                <el-radio-button :label="2">最近7天</el-radio-button>
                                <el-radio-button :label="3">最近30天</el-radio-button>
                                <el-radio-button :label="4">本月</el-radio-button>
                                <el-radio-button :label="5">本年</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="">
                                <el-date-picker
                                    size="small"
                                    v-model="dateRange"
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    value-format="yyyy-MM-dd"
                                    clearable
                                    style="width: 280px">
                                </el-date-picker>
                            </el-form-item>
                    </div>
                </div>

                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="快捷搜索" prop="keyword">
                            <el-input placeholder="" v-model="searchForm.keyword" clearable size="small"/>
                        </el-form-item>
                        <el-button @click="handleSearch" type="primary" size="small">搜索</el-button>
                        <el-button @click="handleReset" type="info" plain size="small">重置</el-button>
                        <!-- <el-button type="text" size="medium" @click="searchFlag = !searchFlag">
                            高级搜索<i class="el-icon--right" :class="searchFlag?'el-icon-arrow-down':'el-icon-arrow-up'"></i>
                        </el-button> -->
                    </div>
                </div>
            </div>
        </el-form>

        <!-- 高级搜索 -->
        <el-form class="mt10" ref="searchForm2" :inline="true" :model="searchForm" v-if="!searchFlag" size="medium" label-width="70px">
            <div class="table_tool_bar_wrap">
                <div class="base_wrap mb10">
                    <div class="left">
                        <el-form-item label="会员名称" prop="memberUsername">
                            <el-input placeholder="" v-model="searchForm.memberUsername" clearable size="small"/>
                        </el-form-item>
                        <el-form-item label="配送方式" prop="memberUsername">
                            <el-input placeholder="" v-model="searchForm.memberUsername" clearable size="small"/>
                        </el-form-item>

                        <el-form-item label="配送方式" prop="deliverType">
                            <el-select v-model="searchForm.deliverType" placeholder="请选择">
                                <el-option label="送货上门" :value="0" />
                                <el-option label="到店自提" :value="1" />
                            </el-select>
                        </el-form-item>
                    </div>
                </div>
            </div>
        </el-form>


        <!-- 表格 -->
        <el-table
            ref="table"
            size="medium"
            height="500"
            empty-text="暂无数据"
            element-loading-text="给我一点时间"
            fit border highlight-current-row
            :stripe="stripe"
            v-loading="loading"
            :data="dataList"
            @selection-change="handleSelection">

            <el-table-column type="selection" width="55" prop="selection" v-if="isHideColumn('selection')" align="center"></el-table-column>
            <el-table-column label="订单信息" prop="orderInfo" v-if="isHideColumn('orderInfo')" align="left" width="260">
                <template slot-scope="scope">
                    <div class="wrap">
                        <div>售后单号:{{scope.row.postSaleSn}}</div>
                        <div>申请时间:{{scope.row.ctime}}</div>
                        <div>订单编号:{{scope.row.orderSn}}</div>
                        <div>订单类型:{{scope.row.orderType | formatOrderType}}</div>
                        <div>下单时间:{{scope.row.orderCtime}}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="申请会员" prop="memberUsername" align="left" width="100" show-overflow-tooltip/>
            <el-table-column label="退款商品" prop="skuName" align="left" width="450">
                <template slot-scope="scope">
                    <div class="skus_wrap">
                        <div class="sku_img">
                            <el-image style="width: 60px; height: 60px" :src="scope.row.goodsSkuImg" :preview-src-list="[scope.row.goodsSkuImg]" fit="contain"></el-image>
                        </div>
                        <div class="sku_name">
                            {{scope.row.goodsSkuName}}
                        </div>
                        <div class="sku_amount">
                            <span>￥{{scope.row.goodsSkuPrice}}</span>
                            <span>X{{scope.row.goodsSkuCount}}</span>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="实付金额" prop="skuRealPrice" align="left" width="120">
                <template slot-scope="scope">
                    <div class="payAmount">￥{{scope.row.goodsSkuRealPrice}}</div>
                </template>
            </el-table-column>
            <el-table-column label="退款金额" prop="returnAmount" align="left" width="120">
                <template slot-scope="scope">
                    <div class="payAmount">￥{{scope.row.returnAmount}}</div>
                </template>
            </el-table-column>
            <el-table-column label="售后状态" prop="status" align="center" width="90">
                <template slot-scope="scope">
                    <el-tag type="primary" size="small" v-if="scope.row.status === 0">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="danger" size="small" v-else-if="scope.row.status === 1">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="primary" size="small" v-else-if="scope.row.status === 2">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="primary" size="small" v-else-if="scope.row.status === 3">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="danger" size="small" v-else-if="scope.row.status === 4">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="primary" size="small" v-else-if="scope.row.status === 5">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="success" size="small" v-else-if="scope.row.status === 6">{{scope.row.status | formatReturnStatus}}</el-tag>
                    <el-tag type="danger" size="small" v-else>-</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作" prop="btn" width="230px" align="left" fixed="right">
                <template slot-scope="scope">

                    <template v-if="scope.row.status == 0">
                        <el-button @click="handleAgree(scope.row)" type="text" size="small">同意</el-button>
                        <el-button @click="handleRejectVisible(scope.row)" type="text" size="small">拒绝</el-button>
                    </template>
                    <template v-if="scope.row.status == 3">
                        <el-button @click="handleConfirmGoods(scope.row)" type="text" size="small">确认收货</el-button>
                        <el-button @click="handleRejectReceiveVisible(scope.row)" type="text" size="small">拒绝收货</el-button>
                    </template>
                    <template v-if="scope.row.status == 5">
                        <el-button @click="handleRefundVisible(scope.row)" type="text" size="small">立即退款</el-button>
                    </template>

                    <el-button @click="handleDetailVisible(scope.row.id)" type="text" size="small">售后详情</el-button>
                    <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>

                </template>
            </el-table-column>

        </el-table>

        <!-- 分页 -->
        <TablePagination
            :total.sync="total"
            :page.sync="searchForm.page"
            :limit.sync="searchForm.limit"
            @handleSizeChange="handleSizeChange"
            @handleCurrentChange="handleCurrentChange"/>

        <!-- 订单详情 -->
        <Detail ref="detail" @refreshDataList="getDataList"></Detail>

        <!-- 拒绝申请 -->
        <el-dialog
            width="30%"
            title="拒绝申请"
            :visible.sync="reject.dialogVisible">

                <el-input
                    type="textarea"
                    :rows="5"
                    placeholder="请输入内容"
                    clearable
                    v-model="reject.rejectReason">
                </el-input>

            <div slot="footer" class="dialog-footer">
                <el-button @click="reject.dialogVisible = false">关 闭</el-button>
                <el-button @click="handleReject" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>
        </el-dialog>

        <!-- 拒绝申请 -->
        <el-dialog
            width="30%"
            title="拒绝收货"
            :visible.sync="rejectReceive.dialogVisible">

                <el-input
                    type="textarea"
                    :rows="5"
                    placeholder="请输入内容"
                    clearable
                    v-model="rejectReceive.rejectReason">
                </el-input>

            <div slot="footer" class="dialog-footer">
                <el-button @click="rejectReceive.dialogVisible = false">关 闭</el-button>
                <el-button @click="handleRejectReceive" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>
        </el-dialog>

        <!-- 立即退款 -->
        <el-dialog
            width="30%"
            title="立即退款"
            :visible.sync="refund.dialogVisible">

            <el-form ref="refund" :model="refund" :rules="rulesRefund" @keyup.enter.native="handleRefund" label-width="120px">
                <el-form-item label="退款单号">
                    {{refund.postSaleSn}}
                </el-form-item>
                <el-form-item label="退款金额">
                    ￥{{refund.returnAmount}}
                </el-form-item>
                <el-form-item label="确认金额" prop="confirmReturnAmount">
                    <el-input v-model="refund.confirmReturnAmount" type="number" @input="numberic(refund,'confirmReturnAmount',$event)" style="width:150px;"></el-input>
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="refund.dialogVisible = false">关 闭</el-button>
                <el-button @click="handleRefund" type="primary" :submitLoading="submitLoading">立即退款</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import TableMixin from '@/mixin/TableMixin'
import TablePagination from '@/views/components/table/table-pagination'
import Detail from './detail'
import { numberic, numberic2, numberic100 } from "@/libs";
export default {
    mixins: [TableMixin],
    components: {
        TablePagination,
        Detail
    },

    data() {
        return {
            dataList: [],
            total: 0,
            loading: false,
            selectionRows: [],
            searchFlag: true,
            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: 'ctime',
                sortType: 'desc',
                keyword: null,
                orderSn: null,
                status: '',
                dateAttr: '',
                dateRange: null
            },
            dateRange: null,

            submitLoading: false,

            // 立即退款
            refund: {
                dialogVisible: false,
                id: null,
                postSaleSn: null,
                returnAmount: null,
                confirmReturnAmount: null
            },

            rulesRefund: {
                confirmReturnAmount: [
                    { required: true, message: '退款金额不能为空', trigger: 'blur' }
                ]
            },

            // 商家拒绝退款
            reject: {
                dialogVisible: false,
                id: null,
                rejectReason: null,
            },

            // 商家拒绝收货
            rejectReceive: {
                dialogVisible: false,
                id: null,
                rejectReason: null,
            },

            numberic

        }
    },

    created() {
    },

    mounted() {
        this.$nextTick(() => {
            this.getDataList()
        })
    },

    methods: {

        /**
         * 订单详情
         */
        handleDetailVisible(id) {
            this.$nextTick(() => {
                this.$refs.detail.init(id)
            })
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/mall/omsorderreturnapply/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 获取表格数据
         */
        async getDataList() {

            if(this.dateRange && this.dateRange.length > 0) {
                this.searchForm.dateAttr = ''
                this.searchForm.dateRange = this.dateRange.join(',')
            } else {
                this.searchForm.dateRange = null
            }

            this.loading = true
            const res = await this.$http({
                url: '/mall/omsorderreturnapply/page',
                method: 'GET',
                params: this.searchForm
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data.records || []
                this.total = res.data.total
            }
            this.loading = false
        },



        /**
         * 商家同意退款
         */
        async handleAgree(row) {
            const confirm = await this.$confirm('确认同意退款？', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/omsorderreturnapply/agree',
                    method: 'GET',
                    params: {id: row.id}
                })
                if (this.ResultMessage(res)) {
                    this.getDataList()
                }
            }
        },


        /**
         * 商家确认收货
         */
        async handleConfirmGoods(row) {
            const confirm = await this.$confirm('商家确认收货', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/omsorderreturnapply/confirmGoods',
                    method: 'GET',
                    params: {id: row.id}
                })
                if (this.ResultMessage(res)) {
                    this.getDataList()
                }
            }
        },

        /**
         * 商家拒绝申请窗口
         */
        handleRejectVisible(row) {
            this.reject.id = row.id
            this.reject.dialogVisible = true
        },

        /**
         * 商家拒绝申请
         */
        async handleReject() {
            const confirm = await this.$confirm('确认拒绝申请？', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/omsorderreturnapply/reject',
                    method: 'POST',
                    data: this.reject
                })
                if (this.ResultMessage(res)) {
                    this.reject.dialogVisible = false
                    this.getDataList()
                }
            }
        },

        /**
         * 商家拒绝收货窗口
         */
        handleRejectReceiveVisible(row) {
            this.rejectReceive.id = row.id
            this.rejectReceive.dialogVisible = true
        },

        /**
         * 商家拒绝收货
         */
        async handleRejectReceive() {
            const confirm = await this.$confirm('确认拒绝收货？', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/omsorderreturnapply/rejectGoods',
                    method: 'POST',
                    data: this.rejectReceive
                })
                if (this.ResultMessage(res)) {
                    this.rejectReceive.dialogVisible = false
                    this.getDataList()
                }
            }
        },

        /**
         * 立即退款窗口
         */
        handleRefundVisible(row) {
            this.refund.id = row.id
            this.refund.postSaleSn = row.postSaleSn
            this.refund.returnAmount = row.returnAmount
            this.refund.confirmReturnAmount = row.returnAmount
            this.refund.dialogVisible = true
        },

        /**
         * 商家发起退款
         */
        async handleRefund() {
            const confirm = await this.$confirm('确认退款？', '提示', { type: 'warning' }).catch(() => {})
            if(confirm) {
                const res = await this.$http({
                    url: '/mall/omsorderreturnapply/refund',
                    method: 'POST',
                    data: this.refund
                })
                if (this.ResultMessage(res)) {
                    this.refund.dialogVisible = false
                    this.getDataList()
                }
            }
        },

        /**
         * 时间查询
         */
        handleChangeDateAttr() {
            this.dateRange = null
            this.searchForm.dateRange = null
            this.getDataList()
        },

        /**
         * 状态查询
         */
        handleChangeStatus() {
            this.getDataList()
        },

        /**
         * 搜索
         */
        handleSearch() {
            this.getDataList()
        },
        /**
         * 重新加载数据表格
         */
        handleReload() {
            this.getDataList()
        },
        /**
         * 重置搜索表单
         */
        handleReset() {
            this.dateRange = null
            this.searchForm.dateRange = null
            this.$refs.searchForm1.resetFields()
            this.$refs.searchForm2.resetFields()
            this.getDataList()
        }

    },

    filters: {
        formatOrderType(value) {
            if (value === 0) {
                return '普通订单';
            } else if (value === 1) {
                return '活动订单';
            } else if (value === 2) {
                return '拼团订单';
            } else if (value === 3) {
                return '秒杀订单';
            } else if (value === 4) {
                return '砍价订单';
            } else if (value === 5) {
                return '核销订单';
            } else {
                return '-';
            }
        },
        formatReturnStatus(value) {
            if (value === 0) {
                return '申请退款';
            } else if (value === 1) {
                return '商家拒绝';
            } else if (value === 2) {
                return '同意退款';
            } else if (value === 3) {
                return '待退货';
            } else if (value === 4) {
                return '拒收货';
            } else if (value === 5) {
                return '待退款';
            } else if (value === 6) {
                return '退款成功';
            } else {
                return '-';
            }
        },
        formatOrderStatus(value) {
            if (value === 0) {
                return '待付款';
            } else if (value === 1) {
                return '待发货';
            } else if (value === 2) {
                return '已发货';
            } else if (value === 3) {
                return '已完成';
            } else if (value === 4) {
                return '已关闭';
            } else {
                return '-';
            }
        },
        formatIsPay(value) {
            if (value === 0) {
                return '未支付';
            } else if (value === 1) {
                return '已支付';
            } else {
                return '-';
            }
        },
        formatDeliverType(value) {
            if (value === 0) {
                return '送货上门';
            } else if (value === 1) {
                return '到店自提';
            } else {
                return '-';
            }
        },
        formatPayType(value) {
            if (value === 0) {
                return '支付宝';
            } else if (value === 1) {
                return '微信';
            } else if (value === 2) {
                return '银联';
            } else if (value === 3) {
                return '货到付款';
            } else {
                return '-';
            }
        },
        formatSourceType(value) {
            if (value === 0) {
                return 'PC订单';
            } else if (value === 1) {
                return '微信订单';
            } else if (value === 2) {
                return 'APP订单';
            } else {
                return '-';
            }
        }
    }

}
</script>

<style lang="scss" scoped>
@import '../../../assets/styles/mixin.scss';
.wrap{
    .payAmount{
        color: red;
    }
}
.skus_wrap{
    display: flex;
    padding: 10px 0;

    .sku_img{
        display: flex;
        justify-content: center;
        align-items: center;
        flex: 1;
    }
    .sku_name{
        display: flex;
        align-items: center;
        flex: 3;
        width: 100px;
        height: 70px;
        @include textoverflow(3);
    }
    .sku_amount{
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: flex-start;
        flex: 1;
        align-items: center;
        span{
            display: block;
        }
    }

    &:first-child{
        padding: 0 0;
    }
    &:last-child{
        border: 0;
        padding: 0 0;
    }
}
.el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
    font-size: 12px;
    margin-left: 10px;
}
.el-icon-arrow-down {
    font-size: 12px;
}
</style>
